﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace TestProject
{
	[AttributeUsage(AttributeTargets.Property)]
	public class TrueNameAttribute : Attribute
	{
		public TrueNameAttribute(string name = "")
		{
			Name = name;
		}
		public string Name { get; set; }
	}

	public class RequestParams
	{
		[TrueName(name: "n")]
		public string Name { get; set; }
		[TrueName(name: "a")]
		public int Age { get; set; }
		[TrueName(name: "s")]
		public int Sex { get; set; }
	}
	public class GetUrl
	{
		public static string GetPropertyName<T>(Expression<Func<T, object>> expr)
		{
			string rtn = "";
			if (expr.Body is UnaryExpression)
			{
				rtn = ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
			}
			else if (expr.Body is MemberExpression)
			{
				rtn = ((MemberExpression)expr.Body).Member.Name;
			}
			else if (expr.Body is ParameterExpression)
			{
				rtn = ((ParameterExpression)expr.Body).Type.Name;
			}
			return rtn;
		}

		public static void Test()
		{
			Console.WriteLine(GetPropertyName<RequestParams>(p => p.Age));
		}


		public void GetComUrl(string baseUrl, params object[] param)
		{
			foreach (var item in param)
			{
				if (item.GetType() == typeof(int))
				{
					if ((int)item > 0)
					{
						baseUrl = baseUrl + "&" + item;
					}
				}
			}
		}
	}
}
